package com.xindaoapp.happypet.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 跳页
 * 
 * @author xindaoapp
 * 
 */
public class SkipPageDialog extends Dialog {
    IRequest<Integer> iRequest;
    int currentPage;
    int totalPage;
    Activity context;
    onSwitchPageChangeListener mChangeListener;

    public SkipPageDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public SkipPageDialog(Activity context, int theme, int currentPage, int totalPage,
            IRequest<Integer> iRequest) {
        super(context, theme);
        this.context = context;
        this.mChangeListener = (PostDetailActivity) context;
        this.iRequest = iRequest;
        this.currentPage = currentPage;
        if (totalPage == 0) {
            totalPage = 1;
        }
        this.totalPage = totalPage;
    }

    public SkipPageDialog(Activity context, int currentPage, int totalPage,
            IRequest<Integer> iRequest) {
        super(context);
        this.context = context;
        this.iRequest = iRequest;
        this.currentPage = currentPage;
        this.totalPage = totalPage;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_skip_page);

        Window window = getWindow();
        LayoutParams lp = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        lp.width = (int) (d.getWidth() * 0.8F);
        // lp.height = (int) (d.getWidth() * 0.8F);
        window.setAttributes(lp);

        // View view = View.inflate(getContext(), R.layout.dialog_skip_page, null);
        //
        // view.setLayoutParams(new LayoutParams((int) (CommonParameter.sScreenWidth * 2.0 / 3),
        // (int) (CommonParameter.sScreenWidth * 2.0 / 3)));
        // setContentView(view);
        // getWindow().setAttributes(
        // new LayoutParams((int) (CommonParameter.sScreenWidth * 2.0 / 3),
        // (int) (CommonParameter.sScreenWidth * 2.0 / 3)));

        ((TextView) findViewById(R.id.page)).setText(String.format("当前%s/%s页", currentPage,
                totalPage));

        final EditText count = (EditText) findViewById(R.id.content);
        count.setFocusable(true);
        showInputMethod();
        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    int page = Integer.parseInt(count.getText().toString());
                    if (1 <= page && page <= totalPage) {
                        iRequest.request(page);
                        currentPage = page;
                        // mChangeListener.setCurrentPage(page);
                        dismiss();
                    } else {
                        Toast.makeText(getContext(), "该页不存在，请重新输入!", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(getContext(), "该页不存在，请重新输入!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void showInputMethod() {
        // 打开软键盘
        InputMethodManager imm =
                (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public interface onSwitchPageChangeListener {
        public void setCurrentPage(int current);
    }
}
